package jp.co.iris_ltd.irisphotoeditor.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PhotoFrameView extends RelativeLayout {
    public static enum FrameType {
        None, Corner, Rectangle,
    }

    private FrameType mFrameType;
    private Drawable mFrameDrawable;
    private ImageView mRightTopImage, mRightBottomImage, mLeftTopImage,
            mLeftBottomImage;

    public PhotoFrameView(Context context) {
        super(context);
        init();
    }

    public PhotoFrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PhotoFrameView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setDrawingCacheEnabled(true);
    }

    public void setFrame(Drawable frameDrawable, FrameType frameType) {
        mFrameType = frameType;
        mFrameDrawable = frameDrawable;

        onFrameChanged();
    }

    public void resetFrame() {
        mFrameType = FrameType.None;
        onFrameChanged();
    }

    public void onFrameChanged() {
        switch (mFrameType) {
        case Rectangle:
            hideCornerFrame();

            setBackgroundDrawable(mFrameDrawable);

            break;
        case Corner:
            setBackgroundDrawable(null);

            createCornerFrame();
            setCornerFrame();
            showCornerFrame();

            break;
        case None:
            break;
        default:
            break;
        }
        invalidate();
    }

    private void createCornerFrame() {

        Context context = getContext();
        removeAllViews();
        if (mRightTopImage == null) {
            mRightTopImage = new ImageView(context);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    200, 200);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            addView(mRightTopImage, lp);
        }

        if (mRightBottomImage == null) {
            mRightBottomImage = new ImageView(context);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    200, 200);
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            addView(mRightBottomImage, lp);
        }

        if (mLeftTopImage == null) {
            mLeftTopImage = new ImageView(context);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    200, 200);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            addView(mLeftTopImage, lp);
        }

        if (mLeftBottomImage == null) {
            mLeftBottomImage = new ImageView(context);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    200, 200);
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            addView(mLeftBottomImage, lp);
        }

    }

    private void setCornerFrame() {
        Bitmap bitmap = ((BitmapDrawable) mFrameDrawable).getBitmap();
        mRightTopImage.setImageBitmap(getRightTopBitmap(bitmap));
        mRightBottomImage.setImageBitmap(getRightBottomBitmap(bitmap));
        mLeftTopImage.setImageBitmap(getLeftTopBitmap(bitmap));
        mLeftBottomImage.setImageBitmap(getLeftBottomBitmap(bitmap));
    }

    private Bitmap getRightTopBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.preScale(-1f, 1f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private Bitmap getRightBottomBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.preScale(-1f, -1f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private Bitmap getLeftTopBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.preScale(1f, 1f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private Bitmap getLeftBottomBitmap(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.preScale(1f, -1f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private void showCornerFrame() {
        mRightTopImage.setVisibility(View.VISIBLE);
        mRightBottomImage.setVisibility(View.VISIBLE);
        mLeftTopImage.setVisibility(View.VISIBLE);
        mLeftBottomImage.setVisibility(View.VISIBLE);
    }

    private void hideCornerFrame() {
        mRightTopImage.setVisibility(View.GONE);
        mRightBottomImage.setVisibility(View.GONE);
        mLeftTopImage.setVisibility(View.GONE);
        mLeftBottomImage.setVisibility(View.GONE);
    }

    private void removeCornerFrame() {
        removeView(mRightTopImage);
        removeView(mRightBottomImage);
        removeView(mLeftTopImage);
        removeView(mLeftBottomImage);
    }
}
